//
//  PhotoLibarayCollectionViewCell.swift
//  MT
//
//  Created by M on 2019/12/22.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class PhotoLibarayCollectionViewCell: UICollectionViewCell {
    
    public var asset: MTAsset? {
        didSet {
            requestPhoto(with: asset)
            guard let asset = asset else { return }
            durationLabel.isHidden = asset.mediaType != .video
            durationLabel.text = asset.duration.transferTime
            disabledCoverView.isHidden = asset.isEnabled
        }
    }
    
    var coverImageViewCornerRadius: CGFloat = 0 {
        didSet {
            if coverImageView.layer.cornerRadius == coverImageViewCornerRadius {
                return
            }
            coverImageView.layer.cornerRadius = coverImageViewCornerRadius
            disabledCoverView.layer.cornerRadius = coverImageViewCornerRadius
        }
    }
    
    /// 图片封面图
    lazy var coverImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        return imgView
    }()
    
    /// 视频时长 Label
    lazy var durationLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCMediumFont(10)
        label.textColor = .white
        label.textAlignment = .right
        return label
    }()
    
    /// 禁选遮罩
    lazy var disabledCoverView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initalizeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ Overrides ----------------------
extension PhotoLibarayCollectionViewCell {
    
    override func prepareForReuse() {
        super.prepareForReuse()
        coverImageView.image = nil
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension PhotoLibarayCollectionViewCell {
    
    private func initalizeUI() {
        contentView.addSubview(coverImageView)
        contentView.addSubview(durationLabel)
        contentView.addSubview(disabledCoverView)
        
        coverImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        durationLabel.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(contentView).offset(-5)
        }
        disabledCoverView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    
}

// MARK: ------------------ 图片读取 ----------------------
extension PhotoLibarayCollectionViewCell {
    
    private func requestPhoto(with asset: MTAsset?) {
        guard let asset = asset else { return }
        MTAssetLibrary.default.requestImage(withAsset: asset.asset, photoWidth: ScreenWidth / 4, progressHandler: nil, errorHandler: nil) { [weak self] (image, info, isDegraded) in
            guard let weakself = self else { return }
            /// 设置图片
            weakself.coverImageView.image = image
        }
    }
    
}
